package com.udp;

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

/**传递基本数据类型
 * 创建服务端*/
/*Basic 中文意思：基础的*/
public class BasicTypeServer02 {
    public static void main(String[] args) {
        DatagramSocket datagramSocket = null;//用来接收与发送数据
        DataInputStream dis = null;//基本数据流
        try{
            //首先实例化DatagramSocket  给定接收端口
            datagramSocket = new DatagramSocket(6666);
            //再定义字节数组
            byte[] b = new byte[1024];
            //封装待接收的数据包
            DatagramPacket dp = new DatagramPacket(b,b.length);
            //接收数据
            datagramSocket.receive(dp);
            //将接收到的数据，通过字节数据流转换为基本数据类型
            dis = new DataInputStream(new ByteArrayInputStream(dp.getData()));

            System.out.println(dis.readLong());

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(dis != null){
                try {
                    dis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(datagramSocket != null){
                datagramSocket.close();
            }
        }
    }
}
